package org.locker.device.controller.admin;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.locker.domain.Position;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.PositionVo;
import org.locker.domain.bo.PositionBo;
import org.locker.service.IPositionService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 场地信息
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/position")
public class PositionController extends BaseController {

    private final IPositionService positionService;

    /**
     * 查询场地信息列表
     */
    @SaCheckPermission("device:position:list")
    @GetMapping("/list")
    public TableDataInfo<PositionVo> list(PositionBo bo, PageQuery pageQuery) {
        return positionService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/all")
    public R<List<Position>> all() {
        return R.ok(positionService.list(Wrappers.<Position>lambdaQuery().select(Position::getPositionId,Position::getName)));
    }

    /**
     * 导出场地信息列表
     */
    @SaCheckPermission("device:position:export")
    @Log(title = "场地信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PositionBo bo, HttpServletResponse response) {
        List<PositionVo> list = positionService.queryList(bo);
        ExcelUtil.exportExcel(list, "场地信息", PositionVo.class, response);
    }

    /**
     * 获取场地信息详细信息
     *
     * @param positionId 主键
     */
    @SaCheckPermission("device:position:query")
    @GetMapping("/{positionId}")
    public R<PositionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long positionId) {
        return R.ok(positionService.queryById(positionId));
    }

    /**
     * 新增场地信息
     */
    @SaCheckPermission("device:position:add")
    @Log(title = "场地信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PositionBo bo) {
        return toAjax(positionService.insertByBo(bo));
    }

    /**
     * 修改场地信息
     */
    @SaCheckPermission("device:position:edit")
    @Log(title = "场地信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PositionBo bo) {
        return toAjax(positionService.updateByBo(bo));
    }

    /**
     * 删除场地信息
     *
     * @param positionIds 主键串
     */
    @SaCheckPermission("device:position:remove")
    @Log(title = "场地信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{positionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] positionIds) {
        return toAjax(positionService.deleteWithValidByIds(List.of(positionIds), true));
    }
}
